#ifdef _WIN32
#include <windows.h>
#endif


#include <GL/gl.h>
#include <GL/glu.h>
#include <glx/glext.h>
#include "example.h"
#include "boglgpwindow.h"
#include "keyboardinterface.h"

Example::Example(BOGLGPWindow* window):
m_window(window)
{
    keyboard = getWindow()->getKeyboard();
}
bool Example::init()
{
 
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glDisable(GL_CULL_FACE);
    glClearColor(0.0f, 0.5f, 1.0f, 0.5f);

	
    m_robot.initialize();
	m_robot2.initialize();
	m_robot3.initialize();

	m_floor.initialize();
	m_pyramid4.initialize4();
	m_pyramid5.initialize5();
    m_rotationAngle = 0.0f;

	posx = 0.0f;
	posy = 0.0f;
	posz = 0.0f;
	rotation = 0.0f;
	counter=0;
    //Return success
    return true;
}

void Example::prepare(float dt)
{
    m_rotationAngle += 10.0f * dt;
    if (m_rotationAngle > 360.0f) {
        m_rotationAngle -= 360.0f;
    }

    m_robot.animate(dt);
	m_robot2.animate(dt);
	m_robot3.animate(dt);
}

void Example::render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
	counter++;
	
	if (keyboard->isKeyHeldDown(KC_UP))
	{
		posz+=0.5f;
	}
	if (keyboard->isKeyHeldDown(KC_DOWN))
	{
		posz-=0.5f;
	}
	if (keyboard->isKeyHeldDown(KC_RIGHT))
	{
		rotation+=0.5f;
	}
	if (keyboard->isKeyHeldDown(KC_LEFT))
	{
		rotation-=0.5f;
	}
	//tranform for camera
	glTranslatef(posx, posy, posz);
	glRotatef(rotation, 0.0f, 1.0f, 0.0f);

    glPushMatrix();	
		m_floor.render();
	glPopMatrix();
	/*
	glPushMatrix();	
		m_pyramid4.render4(m_rotationAngle);
	glPopMatrix();
	
	glPushMatrix();	
		m_pyramid4.render5(m_rotationAngle);
	glPopMatrix();
	*/
	glPushMatrix();	
		m_pyramid4.renderDiamond(m_rotationAngle);
	glPopMatrix();
	
	m_robot.initialize();
	glPushMatrix();							// put current matrix on stack
		glTranslatef(0.0f, 0.0f, -30.0f);	// move to (0, 0, -30)
		glRotatef(m_rotationAngle, 0.0f, 1.0f, 0.0f);	// rotate the robot on its y-axis
		m_robot.render(0.0f, 0.0f, 0.0f);

		m_pyramid4.renderDiamond2(m_rotationAngle);

	glPopMatrix();
	/*
	glPushMatrix();	
		m_pyramid4.renderDiamond2(m_rotationAngle);
	glPopMatrix();
	*/
	/*
	m_robot2.initialize();
	glPushMatrix();							// put current matrix on stack
		glLoadIdentity();					// reset matrix
		glTranslatef(10.0f, 0.0f, -30.0f);	// move to (0, 0, -30)
		glRotatef(m_rotationAngle, 0.0f, 1.0f, 0.0f);	// rotate the robot on its y-axis
		m_robot2.render(0.0f, 0.0f, 0.0f);
	glPopMatrix();

	m_robot3.initialize();
	glPushMatrix();							// put current matrix on stack
		glLoadIdentity();					// reset matrix
		glTranslatef(-10.0f, 0.0f, -30.0f);	// move to (0, 0, -30)
		glRotatef(m_rotationAngle, 0.0f, 1.0f, 0.0f);	// rotate the robot on its y-axis
		m_robot3.render(0.0f, 0.0f, 0.0f);
	glPopMatrix();
	*/
    
}

void Example::shutdown()
{

}

void Example::onResize(int width, int height)
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(52.0f, float(width) / float(height), 1.0f, 1000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}